import Vue from 'vue'
import axios from 'axios';
import { baseUrl } from './config';
console.log(baseUrl);

import {
    Toast
} from 'vant';

// 加载提示
let toast;
export const loadingBegin = () => {
    const vue = new Vue();
    Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true, // 禁用背景点击
        loadingType: 'spinner',
        message: '努力加载中...'
    });
}
export const loadingFinish = () => {
    Toast.clear();
}

// axios降级请求,处理跨域一个请求发两次的问题
//axios.interceptors.request.use(function (config) {
//    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
//    if (config.method === 'post') {
//        config.data = qs.stringify({ ...config.data
//        })
//    }
//    return config;
//}, function (error) {
//    loadinginstace.close();
//    return Promise.reject(error);
//})

export const test = () => {
}

export const ajax = (url, data, type) => {
	// 把baseUrl和传进来的url进行拼接
	url = baseUrl + url;
    let config;
    if (type === 'get') {
        config = {
            method: type,
            params: data,
        }
    } else {
        config = {
            method: type,
            data,
        }
    }
    return axios(url, config);
}

export const fetch = (url, data, suc) => {
    ajax(url, data, 'get').then((res) => {
        let {
            status,
            msg
        } = res.data;
        if (status === 0) {
            suc(res.data.data);
        } else {
            alert(msg);
        }

    });
}
